package com.ohuang.jiaxiaotong.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bunny.android.library.LoadDataLayout;
import com.ohuang.jiaxiaotong.Activity.CourseVideoActivity;
import com.ohuang.jiaxiaotong.Activity.WebActivity;
import com.ohuang.jiaxiaotong.Adapter.CourseRvAdapter;
import com.ohuang.jiaxiaotong.Banner.CourseBean;
import com.ohuang.jiaxiaotong.Banner.DataBean;
import com.ohuang.jiaxiaotong.Banner.ImageHolder;
import com.ohuang.jiaxiaotong.Banner.ImageNetAdapter;
import com.ohuang.jiaxiaotong.MyHttpURL;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.Sys;
import com.ohuang.jiaxiaotong.View.NoScrollRecyclerView;
import com.ohuang.jiaxiaotong.entity.CourseImg;
import com.ohuang.jiaxiaotong.entity.CourseVideo;
import com.youth.banner.Banner;
import com.youth.banner.indicator.RoundLinesIndicator;
import com.youth.banner.util.BannerUtils;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;


public class Home2Fragment extends Fragment {
    private RecyclerView recyclerView;
    private CourseRvAdapter courseRvAdapter;
    private ArrayList<CourseImg> mData;
    private Banner banner;
    private View view;
    private LoadDataLayout loadDataLayout;

    public Home2Fragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_home2, container, false);
        recyclerView = view.findViewById(R.id.rv_home);
//轮播图初始化
        banner = view.findViewById(R.id.banner_home);
        loadDataLayout=view.findViewById(R.id.loadData_home2);
        ImageNetAdapter imageNetAdapter = new ImageNetAdapter(CourseBean.getTestData2());
        imageNetAdapter.setImgOnClickListener(new ImageNetAdapter.ImgOnClickListener() {
            @Override
            public void imgClick(ImageHolder ImageHolder, CourseBean data, int position) {
                if (Sys.isClick()) {
                    Intent intent = new Intent(getContext(), WebActivity.class);
                    intent.putExtra("url", data.WebViewUrl);
                    intent.putExtra("title", data.title);
                    startActivity(intent);
                }
            }
        });
        banner.setAdapter(imageNetAdapter);
        banner.setIndicator(new RoundLinesIndicator(getContext()));
        banner.setIndicatorSelectedWidth((int) BannerUtils.dp2px(15));

        loadDataLayout.showLoading("正在努力加载中", new LoadDataLayout.SetImgCallBack() {
            @Override
            public void setImg(ImageView img) {
                Glide.with(getActivity())
                        .load(R.drawable.gif_loading)
                        .into(img);
            }
        });//显示 load布局 状态正在加载
        httpGetCourseImg();
        return view;
    }

    //获取课程
    private void httpGetCourseImg() {
//创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyHttpURL.twoUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        GithubService service = retrofit.create(GithubService.class);
        service.getCourseImg().enqueue(new Callback<ArrayList<CourseImg>>() {
            @Override
            public void onResponse(Call<ArrayList<CourseImg>> call, Response<ArrayList<CourseImg>> response) {
//加载成功显示课程
                loadDataLayout.showSuccess();
                mData = response.body();
                final GridLayoutManager gridLayoutManager
                        = new GridLayoutManager(getContext(), 2);
                recyclerView.setLayoutManager(gridLayoutManager);
                if (mData != null && getActivity() != null) {
                    courseRvAdapter = new CourseRvAdapter(getContext(), mData);
                    courseRvAdapter.setOnItemClickListener(new CourseRvAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(RecyclerView.ViewHolder holder, int position) {
                            if (Sys.isClick()) {
                                httpGetCourseVideo(mData.get(position).getId() + "");//获取课程视频和图片
                            }
                        }
                    });
                    recyclerView.setAdapter(courseRvAdapter);
                }
            }

            @Override
            public void onFailure(Call<ArrayList<CourseImg>> call, Throwable t) {
           loadDataLayout.showError();
            }
        });

    }

    //获取课程视频和图片
    private void httpGetCourseVideo(String a) {
//创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyHttpURL.twoUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        GithubService service = retrofit.create(GithubService.class);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", a);
        service.getCourseImg(hashMap).enqueue(new Callback<ArrayList<CourseVideo>>() {
            @Override
            public void onResponse(Call<ArrayList<CourseVideo>> call, Response<ArrayList<CourseVideo>> response) {
                Intent intent = new Intent(getContext(), CourseVideoActivity.class);
                intent.putExtra("CourseVideo", response.body().get(0));
                startActivity(intent);
            }

            @Override
            public void onFailure(Call<ArrayList<CourseVideo>> call, Throwable t) {
                System.out.println(t);
            }
        });

    }


    interface GithubService {

        @GET("api/img")
        Call<ArrayList<CourseImg>> getCourseImg();

        @GET("api/video")
//("id")
        Call<ArrayList<CourseVideo>> getCourseImg(@QueryMap HashMap<String, String> deviceInfo);
    }
}
